// 状态码和状态信息
package result

import "gin_test/config"

type errMsg struct {
	msg []string
}

const (
	msgChinese = 0
	msgEnglish = 1
)
const (
	CodeSuccess   = 0     // 成功错误码
	CodeFailed    = 1001  // 默认失败错误码
	CodeUserExist = 10001 // 用户已存在
)

var (
	apiCodeMessage = map[int]errMsg{
		CodeSuccess: {
			msg: []string{"成功", "success"},
		},
		CodeFailed: {
			msg: []string{"失败", "failed"},
		},
		CodeUserExist: {
			msg: []string{"用户已存在", "user not exist"},
		},
	}
)

type Codes struct {
	Success int
	Failed  int
}

// 错误码定义
var ApiCode = &Codes{
	Success: CodeSuccess,
	Failed:  CodeFailed,
}

// GetMessage 供外部调用
func (c *Codes) GetMessage(code int) string {
	msg := ""
	errMsg, ok := apiCodeMessage[code]
	if ok && len(errMsg.msg) > 0 {
		msg = errMsg.msg[msgChinese]
		lang := config.Config.System.Lang
		if lang != "zh-CN" && len(errMsg.msg) > msgEnglish {
			msg = errMsg.msg[msgEnglish]
		}
	}
	return msg
}
